<?php 
class DomArValidatorsTestCase extends DomArTestUnitTestCase {
	
	public function testValidationBasics() {
		
		$item  = new DomArTestClass_ValidatiorBasics();
		
		$item->text = 10;
		$this->assertFalse( $item->validated() );

		$this->assertEqual( substr($item->getLastValidationMessage(),0,12), '1) Bad value');
		$item->setValidated(true);
		$this->assertTrue( $item->validated() );
		
		
		$item->text = 'wrong';
		$this->assertFalse( $item->validated() );
		$this->assertEqual( $item->getLastValidationMessage(),'Value must be "correctValue"!' );
		$item->setValidated(true);
		
		
		$item->text = 'correctValue';
		$this->assertTrue( $item->validated() );
		
		
		$item->text2 = 1000;
		$this->assertFalse( $item->validated() );
		$this->assertEqual( $item->getLastValidationMessage(),'Bad value for text2!' );
		$this->assertEqual( $item->getValidationMessages(),array('Bad value for text2!') );
		
		DomArTestClass_ValidatiorBasics::getTable()->update();
		

		$this->assertFalse( $item->save(), 'Object was saved but had validation errors!');
		
	}
	
	public function testValidationMessages() {
		$item = new DomArTestClass_ValidationMessages();
		$item->text = 'asdf';
		$message = $item->getLastValidationMessage();
		$this->assertEqual($message, 'Bad value is: "asdf" and good value is "myValue"');
	}
	
	public function testValidatorReport() {
		
		DomArOneToOneTestClass::getTable()->update();
	
		$a = new DomArOneToOneTestClass('a');
		$b = new DomArOneToOneChildTestClass('b');
		
		$a->child = $b;
		$b->name = 10;
		
		$this->assertTrue( $a->validated() );
		$this->assertFalse( $b->validated() );
		
		// test validation report appearing and child validation failing.
		$this->assertFalse( $a->save() );
		$report = $a->getValidationReport();
		$this->assertEqual( substr($report->getLastMessage(),0,12), '1) Bad value');
		
		
		// test report setting passing all validation errors
		$report->setValidated( true );
		$this->assertTrue( $a->save() );
		
		
		// test validation report having two objects errors
		$a = new DomArOneToOneTestClass('a');
		$b = new DomArOneToOneChildTestClass('b');
		
		$a->child = $b;
		$a->name = 10;
		$b->name = 10;
		
		$this->assertFalse( $a->validated() );
		$this->assertFalse( $b->validated() );
		
		// test validation report appearing and child validation failing.
		$this->assertFalse( $a->save() );
		$report = $a->getValidationReport();
		$messages = $report->messages;
		$this->assertEqual( substr($messages[0],0,12), '1) Bad value');
		$this->assertEqual( substr($messages[1],0,12), '1) Bad value');
		
	}
	
	public function testUserValidators() {
		$item = new DomArTestClass_ValidatorBasic('a');
		
		$item->user = 5;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->user = 4;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
	}
	
	public function testBasicValidators() {
		
		$item = new DomArTestClass_ValidatorBasic('a');
		
		
		// IN
		$item->value = 1;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->value = 3;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->value = 4;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->value = 5;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->value = 6;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		// <>
		
		$item->ne = 5;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->ne = 11;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->ne = -4;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		
		// >
		
		$item->gt = 4;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->gt = -4;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->gt = 7;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->gt = 9;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		
		// <
		
		$item->lt = 6;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->lt = 5;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->lt = 4;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->lt = 3;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		

		// >=
		
		$item->ge = 1;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->ge = 4;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->ge = 6;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->ge = 5;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		

		// <=
		
		$item->le = 6;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->le = 9;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->le = 1;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->le = 5;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		
		
		$item->more= 6;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->more= 2;
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->more= 5;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->more= 4;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->more= 3;
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
	}
	
	public function testEmail() {
		$item = new DomArTestClass_ValidatorBasic('a');
		
		// test bad
		
		$item->email = 'a@a';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'a@a.a';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'aa@a.aa';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'a@aa.aa';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'a@aa.aa';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'aa@aa.a';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'a_a@a';
		$this->assertFalse( $item->validated() );
		$item->setValidated(true);
		

		// test good
		
		$item->email = 'aa@aa.aa';
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'aa.aa@aa.aa';
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'aa_aa@aa.aa';
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'a_a.a_a@aa.aa';
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		
		$item->email = 'a-a-a-a.a-a@aa.aa';
		$this->assertTrue( $item->validated() );
		$item->setValidated(true);
		

	}
	
	public function testPassword() {

		$this->assertIdentical( DomArValidator_Password::validate('aaAA', 3, 6), 115 );
		
		
		$this->assertIdentical( DomArValidator_Password::validate('aaAA01', 3, 6), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('aaaAAA', 3, 6), 116 );
		$this->assertIdentical( DomArValidator_Password::validate('aaa000', 3, 6), 116 );
		$this->assertIdentical( DomArValidator_Password::validate('012AAA', 3, 6), 116 );
		
		
		$this->assertIdentical( DomArValidator_Password::validate('aaAA00', 2, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('aaaAAA', 2, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('AAA000', 2, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('aaa000', 2, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('aaaaaa', 2, 6 ), 116 );
		
		
		$this->assertIdentical( DomArValidator_Password::validate('aaaaaa', 1, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('AAAAAA', 1, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('000000', 1, 6 ), 0 );
		$this->assertIdentical( DomArValidator_Password::validate('......', 1, 6 ), 116 );
	
	}
	
	public function testLength() {
	
		$this->assertIdentical( DomArValidator_Length::validate('aa',3, 6), 107 );
		$this->assertIdentical( DomArValidator_Length::validate('aaa',3, 6), 0 );
		$this->assertIdentical( DomArValidator_Length::validate('aaaaaaa',3, 6), 107 );
		$this->assertIdentical( DomArValidator_Length::validate('aaaaaa',3, 6), 0 );
		
		
		$this->assertIdentical( DomArValidator_Length::validate('',-1, 6), 0 );
		$this->assertIdentical( DomArValidator_Length::validate('a',-1, 6), 0 );
		$this->assertIdentical( DomArValidator_Length::validate('aaaaaaa',-1, 6), 107 );
		
		
		$this->assertIdentical( DomArValidator_Length::validate('aa',3, -1), 107 );
		$this->assertIdentical( DomArValidator_Length::validate('aaa',3, -1), 0 );
		$this->assertIdentical( DomArValidator_Length::validate('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',3, -1), 0 );
	
	}
	
	

	
	
}
?>